<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item> <i class="el-icon-setting"></i> 顾问列表 </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" class="handle-del mr10" @click="addClick">新增</el-button>
        <el-input placeholder="请输入姓名查询" class="handle-input mr10" v-model="query.kw"></el-input>
        <el-button class="mr10" type="primary" icon="el-icon-search" @click="souClick">搜索</el-button>
        <el-link href="https://element.eleme.io" target="_blank" type="primary">去往顾问聊天后台</el-link>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column type="index" :index="tableIndex" label="序号" width="55" align="center"></el-table-column>
        <el-table-column prop="contractName" label="头像">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.aPic" :preview-src-list="[scope.row.aPic]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="aName" label="姓名"></el-table-column>
        <el-table-column prop="aPhone" label="手机号"></el-table-column>
        <el-table-column prop="aWechart" label="微信号"></el-table-column>
        <el-table-column prop="aScore" label="评分"></el-table-column>
        <el-table-column prop="aNum" label="咨询次数"></el-table-column>
        <el-table-column prop="contractName" label="专业标签">
          <template slot-scope="scope">
            <el-tag size="mini" v-for="item in JSON.parse(scope.row.aTag)">{{item}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" class="red" @click="editClick(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="delClick(scope.row.aId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.page" :page-size="query.limit" :total="query.totalCount"
                       :pager-count="5" @current-change="PageChange" @prev-click="PageChange" @next-click="PageChange"></el-pagination>
      </div>
    </div>
    <el-dialog title="新增顾问（手机号将作为账号）" :visible.sync="adviser.flg" width="40%" @close="formClose">
      <el-form ref="form" :model="adviser.form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="adviser.form.aName"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-image v-if="adviser.form.aPic" style="width: 100px; height: 100px" :src="adviser.form.aPic" :preview-src-list="[adviser.form.aPic]">
          </el-image>
          <el-upload v-if="adviser.form.aPic" action="javascript:;" :before-upload="addupimg" accept=".png,.jpg" :show-file-list="false">
            重新上传
          </el-upload>
          <el-upload v-if="!adviser.form.aPic" action="javascript:;" :before-upload="addupimg" accept=".png,.jpg" list-type="picture-card"
                     :show-file-list="false">
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="微信">
          <el-input v-model="adviser.form.aWechart"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="adviser.form.aPhone"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="adviser.form.aPwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="专业标签">
          <el-checkbox-group v-model="adviser.form.aTag">
            <el-checkbox v-for="item in this.adviser.tagArr" :label="item" name="type"></el-checkbox>
          </el-checkbox-group>
          <el-button icon="el-icon-plus" type="text" @click="pushTag">添加</el-button>
          <div v-if="adviser.tagflg">
            <el-input v-model="adviser.tagText" placeholder="请输入内容（最多7个字）" maxlength="7" style="width:200px;margin-right:10px;"></el-input>
            <el-button type="primary" @click="addTag">确定</el-button>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adviser.flg = false">取 消</el-button>
        <el-button type="primary" @click="formSub" :loading="adviser.load">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { webDadviserInit, webDadviserAdd, webDadviserEdit, webDadviserDel } from '../../../api/index';
import cosUpFile from '../../../utils/cos'
export default {
  data() {
    return {
      query: {
        page: 1,
        limit: 10,
        totalCount: 0,
        kw: '',
      },
      tableData: [],
      adviser: {
        flg: false,
        load: false,
        editid: '',
        tagflg: false,
        tagText: '',
        tagArr: ['行业经验丰富', '专注信用贷'],
        form: {
          aName: '',
          aPhone: '',
          aWechart: '',
          aPic: '',
          aPwd: '',
          aTag: []
        }
      }
    }
  },
  created() {
    this.getInit();
  },
  methods: {
    //初始化
    getInit() {
      webDadviserInit({
        page: this.query.page,
        limit: this.query.limit,
        kw: this.query.kw,
      }).then(res => {
        if (res.code == 0) {
          this.tableData = res.recordList;
          this.query.totalCount = res.totalCount;
        } else {
          this.$message(res.msg);
        }
      })
    },
    //搜索
    souClick() {
      this.query.page = 1;
      this.getInit();
    },
    //序号
    tableIndex(index) {
      return (this.query.page - 1) * this.query.limit + index + 1;
    },
    //页数
    PageChange(num) {
      this.query.page = num;
      this.getInit();
    },
    //上传头像
    addupimg(file) {
      cosUpFile(file, (err, data) => {
        if (err) {
          this.$message("上传失败");
        } else {
          this.$set(this.adviser.form, 'aPic', 'https://' + data.Location);
          // this.$set(this.adviser.form, 'aPic', 'https://img2.baidu.com/it/u=3684117954,695988885&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400');
        }
      })
    },
    //添加专业标签
    pushTag() {
      this.adviser.tagflg = !this.adviser.tagflg;
    },
    addTag() {
      this.adviser.tagArr.push(this.adviser.tagText);
      this.adviser.tagText = '';
    },
    //关闭
    formClose() {
      this.adviser.tagText = '';
      this.adviser.tagflg = false;
      this.adviser.tagArr = ['行业经验丰富', '专注信用贷'];
      this.adviser.editid = '';
    },
    //新增
    addClick() {
      this.adviser.flg = true;
      this.adviser.form = {
        aName: '',
        aPhone: '',
        aWechart: '',
        aPic: '',
        aPwd: '',
        aTag: []
      };
    },
    //编辑
    editClick(item) {
      this.adviser.flg = true;
      this.adviser.editid = item.aId;
      this.adviser.form.aName = item.aName;
      this.adviser.form.aPhone = item.aPhone;
      this.adviser.form.aWechart = item.aWechart;
      this.adviser.form.aPic = item.aPic;
      this.adviser.form.aPwd = item.aPwd;
      (() => {
        JSON.parse(item.aTag).forEach(e => {
          if (this.adviser.tagArr.indexOf(e) == -1) {
            this.adviser.tagArr.push(e);
          }
        })
      })();
      this.adviser.form.aTag = JSON.parse(item.aTag);
    },
    //删除
    delClick(id) {
      webDadviserDel({
        id: id
      }).then(res => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.getInit();
        } else {
          this.$message(res.msg);
        }
      })
    },
    //提交
    formSub() {
      this.adviser.load = true;
      if (this.adviser.editid) {
        webDadviserEdit({
          aId: this.adviser.editid,
          aName: this.adviser.form.aName,
          aPhone: this.adviser.form.aPhone,
          aWechart: this.adviser.form.aWechart,
          aPic: this.adviser.form.aPic,
          aTag: JSON.stringify(this.adviser.form.aTag),
          aPwd: this.adviser.form.aPwd,
        }).then(res => {
          this.adviser.load = false;
          if (res.code == 0) {
            this.$message.success('编辑成功');
            this.adviser.flg = false;
            this.getInit();
          } else {
            this.$message(res.msg);
          }
        })
      } else {
        webDadviserAdd({
          aName: this.adviser.form.aName,
          aPhone: this.adviser.form.aPhone,
          aWechart: this.adviser.form.aWechart,
          aPic: this.adviser.form.aPic,
          aTag: JSON.stringify(this.adviser.form.aTag),
          aPwd: this.adviser.form.aPwd,
        }).then(res => {
          this.adviser.load = false;
          if (res.code == 0) {
            this.$message.success('新增成功');
            this.adviser.flg = false;
            this.getInit();
          } else {
            this.$message(res.msg);
          }
        })
      }
    },
  }
}
</script>
<style scoped>
.handle-input {
    width: 300px;
    display: inline-block;
}
</style>